# -*- coding: utf-8 -*-
import redis
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops

import subprocess


# todo  网站结构更新　
# assert False ,'网站结构更新　'
class MuniuniumaSpider(RedisSpider):
    name = 'muniuniuma'
    redis_key = 'muniuniuma:start_urls'
    v8host = 'http://localhost:5000/'

    con = redis.Redis(host='localhost', port=6379, db=0)
    con.lpush(redis_key, v8host)

    def parse(self, response):

        yield FormRequest(
            url='http://www.mnlmjr.com/index.php?ctl=user&act=dologin&fhash=BvActQgIwcGFRUblhDvPZbFRKlzXAvEWJtklRTwTkdHZCbsxeL',
            formdata={'email': "18582851425",
                      'user_pwd': 'SXB0R3pBcFdGV1JqdVRlTWZLZ21OVXpER3BER1ZBallRRXFBQ25mVUxhVlRyY3dia2MlMjV1NjVCOSUyNXU3RUY0Uml6enk1MTMyNDN3d3clMjV1OEY2RiUyNXU0RUY2',
                      'ajax': "1",
                      'verify': ''
                      },
            dont_filter=True, callback=self.parse_one)

    def parse_one(self, response):
        for i in range(4):
            yield Request(
                url='http://www.mnlmjr.com/index.php?ctl=deals&cid=0&keywords=&level=0&interest=0&months=0&lefttime=0&months_type=0&deal_status=0&city=0&is_company=0&p={}'.format(
                    i + 1),
                dont_filter=True,
                headers={
                    "Referer": "http://www.mnlmjr.com/index.php?ctl=deals",
                    "Upgrade-Insecure-Requests": 1,
                    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
                    "X-DevTools-Emulate-Network-Conditions-Client-Id": "25E2282942C8333E79CAA9C76715462D"
                },
                callback=self.parse_item)

    def parse_item(self, response):
        q = Ops(response)
        num = q.get_num("/html/body/div[4]/div[6]/ul/li")
        for i in range(1, num):
            yield Request(url='http://www.mnlmjr.com{}'.format(
                q.get_attr('/html/body/div[4]/div[6]/ul/li[{}]/span[1]/a'.format(i + 1), 'href')),
                          dont_filter=True,
                          headers={
                              "Referer": "http://www.mnlmjr.com/index.php?ctl=deals",
                              "Upgrade-Insecure-Requests": 1,
                              "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
                          },
                          callback=self.parse_detail)

    def parse_detail(self, response):
        q = Ops(response)

        name = q.get_str('//*[@id="deal-intro"]/div[2]/div[1]/h3/div[1]/span/a')

        if name is None or name == '' or name == []:
            name = q.get_str('//*[@id="deal-intro"]/div[1]/h3/div[1]/span[1]/a')

        amount = q.get_str('//*[@id="deal-intro"]/div[2]/div[2]/div[1]/ul/li[5]/span[2]')
        if isinstance(amount, str):
            amount = amount.replace("￥", "")
        else:
            amount = q.get_str('//*[@id="deal-intro"]/div[2]/div/div[2]/div[1]/div[1]/span/em').replace("￥", "")

        rate = q.get_str('//*[@id="deal-intro"]/div[2]/div[2]/div[1]/div[1]/div[2]/span/em')
        if rate is None or rate == []:
            rate = q.get_str('//*[@id="deal-intro"]/div[2]/div/div[2]/div[1]/div[2]/span/em')

        start = q.get_str('//*[@id="J_deal_tab_box"]/div[2]/div[2]/table/tbody/tr[2]/td[5]').split("\t")[0] or " "

        cycle = q.get_str('//*[@id="deal-intro"]/div[2]/div[2]/div[1]/div[1]/div[3]/span/em')
        try:
            cycle = cycle.replace('个月', '')
        except:
            cycle = 0

        way = q.get_str('//*[@id="deal-intro"]/div[2]/div[2]/div[1]/ul/li[1]/span[2]/em')

        data = str(name) + " " + str(amount) + " " + str(rate) + " " + str(start) + " " + str(cycle) + " " + str(way)
        subprocess.call("echo  '{}' >> 木牛牛马.txt".format(data), shell=True)
